package com.uniriho.lxr0811_7300_bl.ui.uhf

import android.os.Handler
import android.os.Message
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.uniriho.lxr0811_7300_bl.base.BaseApplication
import com.uniriho.lxr0811_7300_bl.base.Constant
import com.uniriho.lxr0811_7300_bl.ble.BluetoothChatService
import com.uniriho.lxr0811_7300_bl.utils.DataTo
import com.uniriho.lxr0811_7300_bl.utils.packetHandles


class ViewModel_UHF : ViewModel() {

    private var startAdd:String? = "20"
    private var index:String ="08"
    private var BQID:String? =null
    private var BQCon:String = "1122334455667788"
    //FF00FF00250144000001000100000000E0040000F55FDD0620081122334455667788B66BFE
    //FF00FF00250144000001000100000000E0040000F55FDD0620081122334455667788AC1DFE

    private val result by lazy {
        MutableLiveData<String>()
    }
    val testResult: LiveData<String> = result

    fun sendData(cmd:String) {
        BluetoothChatService.getInstance().sendData(cmd,result)
    }

    fun UHFRead() {
        BQID = Constant.BQID
        var sendDataByte: ByteArray? = packetHandles.packetHandles("43",false,1,1, DataTo.hexStrToByteArray(BQID+startAdd+index))
        var sendData = DataTo.byteArrayToHexStr(sendDataByte)
        BluetoothChatService.getInstance().sendData(sendData,result)
    }

    fun UHFWrite() {
        if (Constant.BQID.equals("")) {
            BaseApplication.toast("请先寻标签",Toast.LENGTH_SHORT)
            return
        }
        BQID = Constant.BQID
        var sendDataByte: ByteArray? = packetHandles.packetHandles("44",false,1,1,DataTo.hexStrToByteArray(BQID+startAdd+index+BQCon))
        var sendData = DataTo.byteArrayToHexStr(sendDataByte)
        BluetoothChatService.getInstance().sendData(sendData,result)
    }

    fun UHFSetAn() {
//        Constant.UHF_SETANTENNA
        var sendDataByte: ByteArray? = packetHandles.packetHandles("4A",false,1,1,DataTo.hexStrToByteArray("03"))
        var sendData = DataTo.byteArrayToHexStr(sendDataByte)
        BluetoothChatService.getInstance().sendData(sendData,result)
    }
}